<template>
<!-- separator=">" -->
  <el-breadcrumb class="app-breadcrumb" v-if="display">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index) in levelList" :key="item">
        <span v-if="item.redirect==='noRedirect'||index==levelList.length-1" class="no-redirect">{{ $t(item+'.nav') }}</span>
        <a v-else @click.prevent="handleLink(item)">{{ $t(item+'.nav') }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import pathToRegexp from 'path-to-regexp'

export default {
  data() {
    return {
      levelList: [],
      display: true,
    }
  },
  watch: {
    $route(route) {
      if (route.path == '/home') {
        this.display = false;
      } else {
        this.display = true;
      }
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith('/redirect/')) {
        return
      }
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
    if (this.$route.path=='/home') {
      this.display = false;
    }
  },
  methods: {
    getBreadcrumb() {
      // let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
      // const first = matched[0]
      //  if (!this.isDashboard(first)) {
      //   matched = [].concat(matched)
      // }

      // this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
      let path = this.$route.path;
      let pathArr = path.split('/').filter(item=>item!='');
      console.info(pathArr)
      this.levelList = pathArr
    },
    isDashboard(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
    },
    pathCompile(path) {
      // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
      const { params } = this.$route
      var toPath = pathToRegexp.compile(path)
      return toPath(params)
    },
    handleLink() {
      return false;
    }
  }
}
</script>

<style lang="less" scoped>
  .app-breadcrumb {
   flex: 1;
    height: 100%;
    // background-color: #F9FAFB!important;
    padding-left: 8px;
  }
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 16px;
    line-height: 45px;
    /deep/ .el-breadcrumb__separator{
      color: #333333;
    }
    .el-breadcrumb__inner a{
      color:#555555;
    }
    .no-redirect {
      color: #333333;
      font-weight: bold;
      cursor: text;
    }
  }
</style>
